package org.visionlibrary.image.geomtric.lines.direction;

import java.util.List;

import org.visionlibrary.image.geomtric.model.Point2d;
import org.visionlibrary.image.geomtric.model.Vector2d;


/**
 * Klasa realizujaca wyznaczanie dominujacego kierunku zbioru odcinkow jako
 * sredniej wazonej wszystkich odcinkow. 
 */
public class WeightedAverageDirection implements LinesDirection {
	@Override
	public double direction(List<Vector2d> lines) {
		double sum = 0;
		double weight = 0;
		
		for (Vector2d v : lines) {
			Point2d first = v.getFirst();
			Point2d last = v.getLast();
			double direction = Vector2d.angle(first, last, false);
			double w = Point2d.distance(first, last);
			sum += direction * w;
			weight += w;
		}
		
		return sum/weight;
	}
}
